package org.bling.star.server.connect;

import io.netty.channel.*;
import org.bling.star.server.coder.CommandDecoder;
import org.bling.star.server.coder.CommandEncoder;
import org.bling.star.server.connect.handlers.LoggerHandler;
import org.bling.star.server.connect.handlers.StreamServer;
import org.bling.star.server.dispatchers.StrategyFactory;

public class ConnectInitializer extends ChannelInitializer<Channel> {
    private CommandDecoder decoder = new CommandDecoder();
    private CommandEncoder encoder = new CommandEncoder();

    private StrategyFactory strategyFactory;
    public ConnectInitializer(String coderType, StrategyFactory strategyFactory) {
        super();
        //编译类型
        if(coderType != null) {
            decoder = null;//new ProtobufDecoder();
        }
        this.strategyFactory = strategyFactory;
    }

    @Override
    protected void initChannel(Channel ch) throws Exception {
        ChannelPipeline pipeline = ch.pipeline();
        pipeline.addLast(decoder).
                addLast(encoder).
                //addLast(new IdleStateHandler( 0, 0, 60)).
                addLast(new LoggerHandler()).
                addLast(new StreamServer(strategyFactory));
    }

}
